<?
require_once 'BaseAction.php';

class UserController extends TT_Base_Action
{    
	public function indexAction()
	{
		exit();
	}
	
	private function _unpack($obj) {
      if ($obj->data && $data = unserialize($obj->data)) 
      {
        foreach ($data as $key => $value) {
          if (!isset($obj->$key)) {
            $obj->$key = $value;
          }
        }
      }
      return $obj;
    }
	
	//Thuc hien dang nhap dua vao username va password.
	//Tra ve true neu dang nhap thanh cong
	//Tra ve false neu dang nhap that bai
	private function _doLogin($username, $password, $remember){
	
		//Kiem tra username va password trong database
		$u_model = $this->loadModel("Users");
		$user = $u_model->checkUser($username, $password);
		
		//Neu username va password hop le, cho nguoi dung dang nhap
		if ($user){
			//Convert du lieu user sang dang key->value de luu vao registry
			$user = $this->_unpack($user);
            
            //set cookie thanh "da dang nhap" neu nguoi dung chon ghi nho'
            if (isset($remember) && $remember == 1){
                $cookieParams = session_get_cookie_params();
                session_set_cookie_params(
                    TAMTAY_SESSION_TTL,
                    $cookieParams['path'],
                    $cookieParams['domain'],
                    $cookieParams['secure']
                );
            }
            
            $sess = Zend_Session::getSaveHandler();
            $sess->regenerateSessionID();
            
            //Luu tru thong tin ve nguoi dung vao registry de dung lai lan sau
            Zend_Registry::set('user', $user);   
			
			return true; // tra ve dang nhap thanh cong
		}
		else
			return false; //dang nhap that bai
	}
	
    
     public function infoAction()                                              //lay thong tin nguoi dung
    {   
    $this->view->params=$this->_getAllParams(); 
    $u = $this->getUser();
        if ($u->uid == 0){                    //chuyen ve trang login neu chua dang nhap
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }                          
        $u_model = $this->loadModel("Users");
        
        
        $update=$this->_getParam('update');
        $this->view->update=$update;
        
         if ($this->getRequest()->isPost()){ //neu nguoi dung submit form thay doi thong tin
         $new=$this->_getParam('new');
         $a=$u_model->userinfo($u->uid,$new);
         
         }
        
        
        
        
        
        $this->view->user=$u;
        $uid=$u->uid;
                 
         $username=$u_model->checkInfo($uid);
         $this->view->info=$username;
         }

    public function passwordAction()
    {
            $this->view->params=$this->_getAllParams();
        $u = $this->getUser();
        if ($u->uid == 0){            //chuyen ve trang login neu chua dang nhap  
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
        }                    
         $this->view->user=$u;
         $uid=$u->uid;
         $this->view->uid=$uid;
         if ($this->getRequest()->isPost()){ //neu nguoi dung submit form dang nhap
  
             $password_old = $this->_getParam('password_old');
             $password_old = md5($password_old);
             $password_new = $this->_getParam('password_new');
             $password_new = md5($password_new);
            $password = $this->_getParam('password');
            $password = md5($password);
            
                $u_model = $this->loadModel("Users");  
             
                
              if($password_old == $u->pass)          //kiem tra pass cu 
                  {
                        if($password == $password_new)        // kiem tra xac nhan mat khau
                         {
                             $a=$u_model->setpass($uid,$password);
                             $flag_pas=0;
                         }
                         else
                          {
                             $flag_pas=2;
                          }

                  }
              else
                    {
                                             $flag_pas=1; 
                  }
             $this->view->flag_pas=$flag_pas;     
         }
    }
    

    
    public function loginAction()
    {
        $u = $this->getUser();
		
        if ($u->uid > 0){ //neu $u->uid > 0 tuc la user da login
			$this->view->user = $u; //truyen tham so cho View de hien thi ten nguoi dung.
            $this->_helper->getHelper('Redirector')->gotoUrl('view/all?phong_ban=0&page=1');
			return;
        }
		
        if ($this->getRequest()->isPost()){ //neu nguoi dung submit form dang nhap
		
            $username = trim($this->_getParam('username'));
			
            $password = $this->_getParam('password');
            $password = md5($password);
			
            $remember = $this->_getParam('saveme', 0);      
			
            $is_logged_in = $this->_doLogin($username, $password, $remember);
			
            if ( ! $is_logged_in){                                                			
                $this->view->is_login_fail = true;
				$this->view->username = $username;
            } else {
				$u = $this->getUser();
				$this->view->user = $u;
                            $this->_helper->getHelper('Redirector')->gotoUrl('view/all?phong_ban=0&page=1');
            }
            if($u->pass != $password)
                {
                    $this->view->tb=1;
                }

        }
    }
  
	public function logoutAction()
	{
		$u = $this->getUser();
		if ($u->uid == 0){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
		}                    
		Zend_Session::destroy();
		$sess = Zend_Session::getSaveHandler();         		
		$u = $sess->getAnonymousUser();
		Zend_Registry::set('user', $u);
		$ref = $_SERVER['HTTP_REFERER'];
		if ($ref != ''){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit($ref);
		} else {
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
		}
	}
}
?>